package com.source.hotelbath.weight.radiogp;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;

/**
 * Created by kinglong on 2016/2/19.
 */
public class RadioGp extends LinearLayout {


    private OnCheckChangeListener mOnCheckChangeListener;
    private int mCheckId=-1;

    public RadioGp(Context context) {
        super(context);
    }

   /* @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        for(int i=0;i<getChildCount();i++){
            if(!(getChildAt(i) instanceof RadioItem)){
                throw new RuntimeException("RadioGp child must be RadioItem");
            }
        }
    }*/

    public RadioGp(Context context, AttributeSet attrs) {
        super(context, attrs);
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                for (int i = 0; i < getChildCount(); i++) {
                    if (!(getChildAt(i) instanceof RadioItem)) {
                        throw new RuntimeException("RadioGp,child must be RadioItem");
                    }
                    ((RadioItem) getChildAt(i)).setOnTouchListener(new OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if(inRangeOfView(v,event)){
                                if(event.getAction()== MotionEvent.ACTION_UP){
                                    if(v.getId()>0){
                                        if(null!=mOnCheckChangeListener&&mOnCheckChangeListener.onCheckChaenged(v.getId())){
                                            mCheckId=v.getId();
                                            setcheck(mCheckId);
                                        }else {
                                            setcheck(mCheckId);
                                        }
                                    }
                                }
                                if(event.getAction()== MotionEvent.ACTION_DOWN){
                                    ((RadioItem) v).setchecked(true);
                                }
                                if(event.getAction()== MotionEvent.ACTION_CANCEL){
                                    setcheck(mCheckId);
                                }
                                return true;
                            }else{
                                setcheck(mCheckId);
                                return false;
                            }
                        }
                    });

                }
            }
        });
    }
    private boolean inRangeOfView(View view, MotionEvent ev){
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        if(ev.getRawX() < x || ev.getRawX() > (x + view.getWidth()) || ev.getRawY() < y || ev.getRawY() > (y + view.getHeight())){
            return false;
        }
        return true;
    }


    public void setcheck(int childId) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i).getId() == childId) {
                ((RadioItem) getChildAt(i)).setchecked(true);
                mCheckId=childId;
            } else {
                ((RadioItem) getChildAt(i)).setchecked(false);
            }
        }
    }


    public OnCheckChangeListener getOnCheckChangeListener() {
        return mOnCheckChangeListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener mOnCheckChangeListener) {
        this.mOnCheckChangeListener = mOnCheckChangeListener;
    }

    public interface OnCheckChangeListener {
        boolean onCheckChaenged(int id);
    }

}
